﻿
namespace SilverlightTowns.Web
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.ComponentModel.DataAnnotations;
    using System.Data;
    using System.Linq;
    using System.ServiceModel.DomainServices.EntityFramework;
    using System.ServiceModel.DomainServices.Hosting;
    using System.ServiceModel.DomainServices.Server;


    // Implements application logic using the GeographyDBEntities context.
    // TODO: Add your application logic to these methods or in additional methods.
    // TODO: Wire up authentication (Windows/ASP.NET Forms) and uncomment the following to disable anonymous access
    // Also consider adding roles to restrict access as appropriate.
    // [RequiresAuthentication]
    [EnableClientAccess()]
    public class DomainServiceTown : LinqToEntitiesDomainService<GeographyDBEntities>
    {

        // TODO:
        // Consider constraining the results of your query method.  If you need additional input you can
        // add parameters to this method or create additional query methods with different names.
        // To support paging you will need to add ordering to the 'Countries' query.
        public IQueryable<Country> GetCountries()
        {
            return this.ObjectContext.Countries;
        }

        public void InsertCountry(Country country)
        {
            if ((country.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(country, EntityState.Added);
            }
            else
            {
                this.ObjectContext.Countries.AddObject(country);
            }
        }

        public void UpdateCountry(Country currentCountry)
        {
            this.ObjectContext.Countries.AttachAsModified(currentCountry, this.ChangeSet.GetOriginal(currentCountry));
        }

        public void DeleteCountry(Country country)
        {
            if ((country.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(country, EntityState.Deleted);
            }
            else
            {
                this.ObjectContext.Countries.Attach(country);
                this.ObjectContext.Countries.DeleteObject(country);
            }
        }

        // TODO:
        // Consider constraining the results of your query method.  If you need additional input you can
        // add parameters to this method or create additional query methods with different names.
        // To support paging you will need to add ordering to the 'Towns' query.
        public IQueryable<Town> GetTowns()
        {
            return this.ObjectContext.Towns;
        }

        public void InsertTown(Town town)
        {
            if ((town.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(town, EntityState.Added);
            }
            else
            {
                this.ObjectContext.Towns.AddObject(town);
            }
        }

        public void UpdateTown(Town currentTown)
        {
            this.ObjectContext.Towns.AttachAsModified(currentTown, this.ChangeSet.GetOriginal(currentTown));
        }

        public void DeleteTown(Town town)
        {
            if ((town.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(town, EntityState.Deleted);
            }
            else
            {
                this.ObjectContext.Towns.Attach(town);
                this.ObjectContext.Towns.DeleteObject(town);
            }
        }
    }
}


